<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="150"
			label-align="left">
			<u-form-item label="报销编号" v-if="judgeShow('ta002')">
				<u-input v-model="dataForm.ta002" disabled placeholder="提交保存后系统自动生成"></u-input>
			</u-form-item>
			<u-form-item label="报销日期" v-if="judgeShow('ta004')">
				<lys-date v-if="showta004" type="1" @cancleTime="showta004 = false"
					@submitTime="submitta004"></lys-date>
				<view style="display: flex; justify-content: space-between; width: 100%">
					<view>
						{{ dataForm.ta004 }}
					</view>
					<view>
						<u-icon name="arrow-down-fill" color="#ccc" @click="showta004 = true"></u-icon>
					</view>
				</view>
			</u-form-item>
			<u-form-item label="报销类型" v-if="judgeShow('ta006')">
				<u-radio-group v-model="dataForm.ta006">
					<u-radio v-for="item in ta006Options" :key="item.fullName" :name="item.fullName">
						{{ item.fullName }}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="报销人员" v-if="judgeShow('ta005')">
				<jnpf-user-select v-model="dataForm.ta005" @input="changeName" />
			</u-form-item>
			<u-form-item label="报销部门" v-if="judgeShow('ta003')">
				{{ dataForm.ta024 }}
			</u-form-item>
			<u-form-item label="借款余额" v-if="judgeShow('ta019')">
				<u-input v-model="dataForm.ta019" disabled></u-input>
			</u-form-item>
			<u-tabs-swiper activeColor="#1890ff" ref="tabs" :list="tabsList" :current="current" @change="change"
				:is-scroll="false" :show-bar="false"></u-tabs-swiper>
			<view class="jnpf-table" style="margin-top: 24rpx" v-if="current === 0">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx" v-for="(item, i) in dataForm.tableField115"
					:key="i">
					<u-form-item label="序号" style="background-color: aqua">
						{{ i + 1 }}
					</u-form-item>
					<u-form-item label="申请单号" v-if="judgeShow('tableField115-tb010')">
						<view style="display: flex; justify-content: space-between; width: 100%">
							<view> {{ item.tb010 }} </view>
							<view>
								<u-icon name="arrow-down-fill" color="#ccc" @click="chioceOastgBox(i)"></u-icon>
							</view>
						</view>
					</u-form-item>
					<u-form-item label="费用类型" v-if="judgeShow('tableField115-tb006')">
						<view style="display: flex; justify-content: space-between; width: 100%">
							<view> {{ item.tb006Name }} </view>
							<view>
								<u-icon name="arrow-down-fill" color="#ccc"
									@click="chioceOastdBox(i, 'tableField115')"></u-icon>
							</view>
						</view>
					</u-form-item>
					<u-form-item label="金额" v-if="judgeShow('tableField115-tb004')">
						<u-input v-model="item.tb004" @blur="residueSumChange(item, i)" type="number"></u-input>
					</u-form-item>
					<u-form-item label="预算总额" v-if="judgeShow('tableField115-tb013')">
						<u-input v-model="item.tb013" disabled></u-input>
					</u-form-item>
					<u-form-item label="剩余额度" v-if="judgeShow('tableField115-tb014')">
						<u-input v-model="item.tb014" disabled></u-input>
					</u-form-item>
					<u-form-item label="事由" v-if="judgeShow('tableField115-tb015')">
						<u-input v-model="item.tb015" :disabled="
                judgeWrite('tableField115') || judgeWrite('tableField115-tb015')
              "></u-input>
					</u-form-item>
					<u-form-item label="附件" label-width="80">
						<jnpf-file v-model="item.tb016" ref="jnpfFiletb016" />
					</u-form-item>
					<u-button type="error" plain :ripple="true" @click="delHandleOastbEntityList(i)">删除</u-button>
				</view>
				<u-button type="primary" @click="addHandleOastbEntityList()"
					v-if="!setting.readonly && !judgeWrite('tableField115')">新增</u-button>
			</view>
			<view class="jnpf-table" style="margin-top: 24rpx" v-if="current === 1">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx" v-for="(item, i) in dataForm.oastjList"
					:key="i">
					<u-form-item label="序号" style="background-color: aqua">
						{{ i + 1 }}
					</u-form-item>
					<u-form-item label="费用类型" v-if="judgeShow('oastjList-tj004')">
						<view style="display: flex; justify-content: space-between; width: 100%">
							<view>
								{{ item.tj004Name }}
							</view>
							<view>
								<u-icon name="arrow-down-fill" color="#ccc"
									@click="chioceOastdBox(i, 'oastjList')"></u-icon>
							</view>
						</view>
					</u-form-item>
					<u-form-item label="金额" v-if="judgeShow('oastjList-tj006')">
						<u-input v-model="item.tj006" :disabled="
                judgeWrite('oastjList') || judgeWrite('oastjList-tj006')
              " @blur="invoiceChange(item, i)" type="number"></u-input>
					</u-form-item>
					<u-form-item label="专票税额" v-if="judgeShow('oastjList-tj007')">
						<u-input v-model="item.tj007" :disabled="
                judgeWrite('oastjList') || judgeWrite('oastjList-tj007')
              " @blur="invoiceChange(item, i)" type="number"></u-input>
					</u-form-item>
					<u-form-item label="价税合计" v-if="judgeShow('oastjList-tj008')">
						<u-input v-model="item.tj008" type="number" disabled></u-input>
					</u-form-item>
					<u-form-item label=" 备注" v-if="judgeShow('oastjList-tj009')">
						<u-input v-model="item.tj009" :disabled="
                judgeWrite('oastjList') || judgeWrite('oastjList-tj009')
              "></u-input>
					</u-form-item>
					<u-form-item label="附件" label-width="80">
						<jnpf-file v-model="item.tj010" />
					</u-form-item>
					<u-button type="error" plain :ripple="true" @click="delHandleOastjEntityList(i)">删除</u-button>
				</view>
				<u-button type="primary" @click="addHandleOastjEntityList()">新增</u-button>
			</view>
			<u-divider type="success" height="80rpx">分割线 </u-divider>
			<u-form-item label="报销金额合计" v-if="judgeShow('ta020')" label-width="250">
				<u-input v-model="dataForm.ta020" disabled></u-input>
			</u-form-item>
			<u-form-item label="报销金额（大写）" v-if="judgeShow('ta021')" label-width="250">
				<u-input v-model="dataForm.ta021" disabled></u-input>
			</u-form-item>
			<u-form-item label="发票金额合计" v-if="judgeShow('ta022')" label-width="250">
				<u-input v-model="dataForm.ta022" disabled></u-input>
			</u-form-item>
			<u-form-item label="发票金额（大写）" v-if="judgeShow('ta023')" label-width="250">
				<u-input v-model="dataForm.ta023" disabled></u-input>
			</u-form-item>
			<u-form-item label="实际报销额" v-if="judgeShow('ta013')" label-width="250">
				<u-input v-model="dataForm.ta013" disabled type="number"></u-input>
			</u-form-item>
			<u-form-item label="支付现金金额" v-if="judgeShow('ta014')" label-width="250">
				<u-input v-model="dataForm.ta014" disabled @blur="paycCashChange" type="number"></u-input>
			</u-form-item>
			<u-form-item label="退还预支款金额" v-if="judgeShow('ta015')" label-width="250">
				<u-search v-model="dataForm.ta015" disabled bg-color="#fff" search-icon='' placeholder='退还预支款金额'
					@custom="getRefund" @search='getRefund' @blur="refundSumChange"></u-search>
			</u-form-item>
			<u-form-item label="收款人(单位)" v-if="judgeShow('ta016')" label-width="250">
				<u-search v-model="dataForm.ta016" :disabled="judgeWrite('ta016')" bg-color="#fff" search-icon=''
					placeholder='收款人(单位)' @custom="getPersonBankInfo" @search='getPersonBankInfo'></u-search>
			</u-form-item>
			<u-form-item label="开户行" v-if="judgeShow('ta017')" label-width="250">
				<u-input v-model="dataForm.ta017" :disabled="judgeWrite('ta017')"></u-input>
			</u-form-item>
			<u-form-item label="银行账户" v-if="judgeShow('ta018')" label-width="250">
				<u-input v-model="dataForm.ta018" :disabled="judgeWrite('ta018')"></u-input>
			</u-form-item>
		</u-form>
		<u-toast ref="uToast" />
		<u-popup v-model="popupshow" mode="bottom" height="50vh">
			<view class="content">
				<scroll-view scroll-y="true" style="height: 800rpx">
					<zsf-table :radio="true" @getRow="getSelectData" ref="product" :title="productionQueueTitle"
						:data="tf004List"></zsf-table>
				</scroll-view>
				<view class="confrim-btn">
					<u-button @click="close">确定</u-button>
				</view>
			</view>
		</u-popup>
		<u-popup v-model="oastgBoxVisible" mode="bottom" height="50vh">
			<view class="content">
				<scroll-view scroll-y="true" style="height: 800rpx">
					<zsf-table :radio="true" @getRow="selectOastgList" ref="product" :title="OastgTitle"
						:data="OastgList"></zsf-table>
				</scroll-view>
				<view class="confrim-btn">
					<u-button @click="oastgBoxClose">确定</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import comMixin from "../mixin";
	import {
		getGoodsList,
		getCustomerList,
		Info
	} from "@/api/apply/order";

	import {
		postOasta,
		putOaste,
		BomcbListInit,
		getOastaBudgetNo,
		getOastdList,
	} from "@/api/workFlow/flowBefore";
	import {
		getDictionaryDataSelector,
		getDataInterfaceDataInfo,
		getUserAll,
		GetExpenseBudget,
		GetExpenseApplicationsForCurrentUser,
	} from "@/api/common";
	import {
		UserSettingInfo
	} from "@/api/user";
	export default {
		mixins: [comMixin],
		watch: {
			"dataForm.tableField115": {
				handler(newVal, oldVal) {
					this.realCalculateSum();
				},
				deep: true, //深度监听
			},
			"dataForm.oastjList": {
				handler(newVal, oldVal) {
					console.log("监听", newVal);
					this.billCalculateSum();
				},
				deep: true, //深度监听
			},
		},
		data() {
			return {
				tabsList: [{
						name: "真实情况",
					},
					{
						name: "发票情况",
					},
				],

				OastgList: [],
				show: false,
				refundVisible: false,
				oastgBoxVisible: false,
				popupshow: false,
				showtf003: false,
				showta004: false,
				showte006: false,
				showte008: false,
				showte009: false,
				productionQueueTitle: [{
						title: "序号",
						key: "sid",
						width: "80",
					},
					{
						title: "费用类型编号",
						key: "td001",
						width: "80",
					},
					{
						title: "费用类型名称",
						key: "td002",
						width: "80",
					},
				],
				OastgTitle: [{
						title: "序号",
						key: "sid",
						width: "80",
					},
					{
						title: "单号",
						key: "tg003",
						width: "80",
					},
					{
						title: "金额",
						key: "tk004",
						width: "80",
					},
					{
						title: "类型",
						key: "tk003Name",
						width: "80",
					},
				],

				dataForm: {
					ta001: "",
					flowId: "",
					ta002: undefined,
					ta003: undefined,
					ta004: undefined,
					ta005: undefined,
					ta006: undefined,
					//ta006Name: undefined,
					ta007: 0,
					ta008: "零",
					ta010: undefined,
					ta011: undefined,
					ta009: undefined,
					ta012: [],
					ta005Name: undefined,
					ta003Name: undefined,
					ta013: 0,
					ta014: 0,
					ta015: 0,
					ta016: undefined,
					ta017: undefined,
					ta018: undefined,
					ta019: 0,
					ta020: 0,
					ta021: "零",
					ta022: 0,
					ta023: "零",
					ta024: undefined, //部门名称
					ta025: "N", //未超预算
					ta016: undefined,
					tableField115: [

					],
					oastjList: [], //发票情况
					flowState: 0, //提交0 保存1
					oastoList: []
				},
				id: "",
				tf004List: [],
				tj004: "",
				tj004Name: "",
				tb006: "",
				tb006Name: "",
				index: "",
				lastItemId: "",
				lastItemName: "",
				ta006Options: [
					// {
					// 	fullName: "材料报销",
					// 	id: "1",
					// },
					// {
					// 	fullName: "对公付款",
					// 	id: "2",
					// },
					// {
					// 	fullName: "日常报销",
					// 	id: "3",
					// },
				],
				options: [{
					text: "删除",
					style: {
						backgroundColor: "#dd524d",
					},
				}, ],
				current: 0,
				currentType: 0,
				ta003: null,
				tb010: null,
				oastgDataList: [],
				List: [],

			};
		},
		onUnload() {},
		methods: {
			getta006Options() {
				getDictionaryDataSelector("594052415976791365").then((res) => {
					this.ta006Options = res.data.list;
				});
			},
			getRefund() {
				let config = {
					oastoList: this.dataForm.oastoList,
					ta005: this.dataForm.ta005,
					isAdd: true,
				}
				uni.setStorageSync('refundObject', JSON.stringify(config));
				this.$nextTick(() => {
					uni.navigateTo({
						url: `/pages/workFlow/workFlowForm/oasta/com/refund`
					});
				})
			},
			selectPersonBankInfoList(data) {
				this.dataForm.ta016 = data.ta016; //收款人（单位）
				this.dataForm.ta017 = data.ta017; //开户行
				this.dataForm.ta018 = data.ta018; //银行账户
			},
			getPersonBankInfo() {
				uni.navigateTo({
					url: `/pages/workFlow/workFlowForm/oasta/com/index`
				});
			},
			addHandleOastbEntityList() {
				if (!this.dataForm.ta005) {
					return this.$refs.uToast.show({
						title: "报销人员不能为空！",
						type: "error",
					});
				}
				if (!this.dataForm.ta003) {
					return this.$refs.uToast.show({
						title: "报销部门不能为空！",
						type: "error",
					});
				}
				if (!this.dataForm.ta004) {
					return this.$refs.uToast.show({
						title: "报销日期不能为空！",
						type: "error",
					});
				}
				let item = {
					tb003: null,
					tb004: 0,
					tb005: null,
					tb006: null,
					tb007: null,
					tb008: 0,
					tb009: 0,
					tb010: null,
					tb011: 0,
					tb012: 0,
					tb006Name: null,
					tb013: 0,
					tb014: 0,
					tb015: null,
					tb016: [],
				};
				this.dataForm.tableField115.push(item);
			},
			addHandleOastjEntityList() {
				let item = {
					tj003: null,
					tj004: null,
					tj006: 0,
					tj007: 0,
					tj008: 0,
					tj009: null,
					tj010: [],
					tj004Name: null,
				};
				this.dataForm.oastjList.push(item);
			},
			getUserInfo() {
				UserSettingInfo().then((res) => {
					console.log('res', res);
					this.dataForm.ta005 = res.data.id; //用户
					this.dataForm.ta003 = res.data.organizeId; //部门
					this.ta003 = res.data.id;
					this.dataForm.ta024 = res.data.department; //部门名称
				});
			},
			//获取费用申请单列表
			chioceOastgBox(index) {
				var queryParam = {
					tg002: this.ta003,
					currentPage: 1,
					pageSize: 500,
					sort: "asc",
					sidx: "TG001",
				};
				this.index = index; //当前下标
				this.oastgBoxVisible = true;
				GetExpenseApplicationsForCurrentUser(queryParam).then((res) => {
					this.OastgList = res.data.list.map((item, index) => {
						return {
							sid: index + 1,
							...item,
						};
					});
				});
			},
			//真实情况 选择费用申请单
			oastgBoxClose() {
				this.oastgBoxVisible = false;
				let list = [{
					...this.oastgDataList,
				}, ];
				console.log('list', list);
				this.dataForm.tableField115.splice(this.index, 1);
				for (let i = 0; i < list.length; i++) {
					const e = list[i];
					var queryParam = {
						te003: this.dataForm.ta003, //部门
						te005: this.dataForm.ta004, //日期
						tf004: e.tk003, //费用类型
					};
					GetExpenseBudget(queryParam).then((res) => {
						let item = {
							tb004: e.tk004, //金额
							tb006: e.tk003, //费用类型（选会计科目树形图，只能选末级）
							tb006Name: e.tk003Name,
							tb010: this.tb010, //费用申请单单号（可以取单）
							tb013: res.data.sumTf005, //预算总金额
							tb014: res.data.residualCredit, //剩余额度
						};
						this.dataForm.tableField115.push(item);
					});
				}
			},
			// 真实/发票，费用类型确认按钮
			close() {
				this.popupshow = false;
				if (this.currentType == 0) {
					this.dataForm.tableField115.splice(this.index, 1);
					for (let i = 0; i < this.List.length; i++) {
						const e = this.List[i];
						var queryParam = {
							te003: this.dataForm.ta003, //部门
							te005: this.dataForm.ta004, //日期
							tf004: e.td001, //费用类型
						};
						GetExpenseBudget(queryParam).then((res) => {
							let item = {
								tb004: 0, //金额
								tb006: e.td001, //费用类型
								tb006Name: e.td002,
								tb010: this.tb010, //费用申请单单号（可以取单）
								tb013: res.data.sumTf005, //预算总金额
								tb014: res.data.residualCredit, //剩余额度
							};
							this.dataForm.tableField115.push(item);
							this.dataForm.tableField115[this.index].tb006Name = this.tb006Name;
							this.dataForm.tableField115[this.index].tb006 = this.tb006;
							console.log('this.dataForm.tableField115', this.dataForm.tableField115);
						});
					}
				} else {
					this.dataForm.oastjList[this.index].tj004Name = this.tj004Name;
					this.dataForm.oastjList[this.index].tj004 = this.tj004;
					console.log('this.dataForm.oastjList', this.dataForm.oastjList);
				}
			},
			//单身验证
			verify() {
				console.log("this.dataForm", this.dataForm);
				let isOk = true;
				this.dataForm.ta025 = "N"; //默认未超预算
				//真实情况
				for (let i = 0; i < this.dataForm.tableField115.length; i++) {
					const e = this.dataForm.tableField115[i];
					console.log('e', e);
					if (this.jnpf.isEmpty(e.tb004)) {
						this.$refs.uToast.show({
							title: "真实情况-金额不能为空！",
							type: "error",
						});
						isOk = false;
						break;
					}
					if (e.tb016 == null) this.dataForm.tableField115[i].tb016 = [];
					if (this.jnpf.isEmpty(e.tb006)) {
						this.$refs.uToast.show({
							title: "真实情况-费用类型不能为空！",
							type: "error",
						});
						isOk = false;
						break;
					}
					//判断金额是否超出剩余额度
					if (parseFloat(e.tb004 || 0) > parseFloat(e.tb014 || 0)) {
						this.dataForm.ta025 = "Y"; //超预算
					}
				}
				//发票情况
				for (let i = 0; i < this.dataForm.oastjList.length; i++) {
					const e = this.dataForm.oastjList[i];
					if (this.jnpf.isEmpty(e.tj007) || this.jnpf.isEmpty(e.tj006)) {
						this.$refs.uToast.show({
							title: "发票情况-金额/专票税额不能为空",
							type: "error",
						});
						isOk = false;
						break;
					}
					if (e.tj010 == null) this.dataForm.oastjList[i].tj010 = [];
					if (this.jnpf.isEmpty(e.tj004)) {
						this.$refs.uToast.show({
							title: "发票情况-费用类型不能为空",
							type: "error",
						});

						isOk = false;
						break;
					}
				}
				return isOk;
			},
			changeName(id) {

				getUserAll().then((res) => {
					let item = res.data.filter((o) => o.id === id)[0];

					this.dataForm.ta003 = item.departmentId; //部门编号
					this.ta003 = item.id;
					this.dataForm.ta024 = item.department; //部门名称
				});
			},
			roundToDecimal(num) {
				return parseFloat(num).toFixed(2);
			},
			//计算价税合计
			invoiceChange(row, i) {
				//价税合计=金额+专票税额
				this.dataForm.oastjList[i].tj008 = this.roundToDecimal(parseFloat(row.tj006 || 0) + parseFloat(row.tj007 ||
					0));
			},
			//计算剩余额度
			residueSumChange(row, i) {
				if (parseFloat(row.tb013) >= parseFloat(row.tb004)) {
					//剩余额度=预算总额-金额
					this.dataForm.tableField115[i].tb014 =
						this.roundToDecimal(parseFloat(row.tb013) - parseFloat(row.tb004));
				} else {
					this.dataForm.tableField115[i].tb014 = 0;
				}
			},
			//获取真实情况-单身金额的金额合计
			realCalculateSum() {
				let total = 0;
				this.dataForm.tableField115.forEach((item) => {
					let tb004 = item.tb004 && !isNaN(item.tb004) ? Number(item.tb004) : 0;
					total = total + tb004;
				});
				this.$set(this.dataForm, "ta020", this.roundToDecimal(total));
				var ta021 = this.jnpf.capAmountToString(this.dataForm.ta020);
				this.$set(this.dataForm, "ta021", ta021);
				var ta013 = this.roundToDecimal(this.dataForm.ta020);
				this.$set(this.dataForm, "ta013", ta013);
				this.actualAndLoanChange();
			},
			//获取发票情况-单身金额的金额合计
			billCalculateSum() {
				let total = 0;
				this.dataForm.oastjList.forEach((item) => {
					let tj008 = item.tj008 && !isNaN(item.tj008) ? Number(item.tj008) : 0;
					total = total + tj008;
				});
				this.dataForm.ta022 = this.roundToDecimal(total)
				this.$set(this.dataForm, "ta022", this.roundToDecimal(total));
				// 大写
				var ta023 = this.jnpf.capAmountToString(this.dataForm.ta022);
				this.dataForm.ta023 = ta023;
				this.$set(this.dataForm, "ta023", ta023);
			},
			//借款余额或实际报销额改变时
			actualAndLoanChange() {
				//退款支款金额
				let isAdd = !this.dataForm.ta001 ? true : false;
				if (isAdd) {
					if (parseFloat(this.dataForm.ta013) < parseFloat(this.dataForm.ta019)) {
						this.dataForm.ta015 = parseFloat(this.dataForm.ta013).toFixed(2);
					} else {
						this.dataForm.ta015 = parseFloat(this.dataForm.ta019).toFixed(2);
					}
				}
				this.refundSumChange();
			},
			//支付现金金额改变时
			paycCashChange() {
				//退还预支款金额=实际报销额-支付现金金额
				this.dataForm.ta015 =
					this.roundToDecimal(parseFloat(this.dataForm.ta013 || 0) -
						parseFloat(this.dataForm.ta014 || 0));
			},
			//退还预支款金额改变时
			refundSumChange() {
				//支付现金金额=实际报销额-退还预支款金额
				this.dataForm.ta014 =
					this.roundToDecimal(parseFloat(this.dataForm.ta013 || 0) -
						parseFloat(this.dataForm.ta015 || 0));
			},
			change(e) {
				this.current = e;
				this.currentType = e;
			},
			submitta004(time, type, timeInt) {
				let ta004 = time.split("-");
				this.dataForm.ta004 = ta004[0] + ta004[1] + ta004[2];
				this.showta004 = false;
			},
			getSelectData(e) {
				if (this.currentType == 0) {
					this.List = [{
						...e.data,
					}];
					console.log('e', e);
					this.tb006 = e.data.td001;
					this.tb006Name = e.data.td002;
				} else {
					this.tj004 = e.data.td001;
					this.tj004Name = e.data.td002;
				}
			},
			// 真实情况点击当行赋值
			selectOastgList(e) {
				this.oastgDataList = e.data;
				this.tb010 = e.data.tg003
			},
			// 删除
			delHandleOastjEntityList(index) {
				uni.showModal({
					title: "提示",
					content: "您确定要删除当前数据吗？",
					success: (res) => {
						if (res.confirm) {
							this.dataForm.oastjList.splice(index, 1);
							this.$u.toast(`删除了发票情况第${index + 1}条`);
						}
					},
				});
			},
			// 删除
			delHandleOastbEntityList(index) {
				uni.showModal({
					title: "提示",
					content: "您确定要删除当前数据吗？",
					success: (res) => {
						if (res.confirm) {
							this.dataForm.tableField115.splice(index, 1);
							this.$u.toast(`删除了真实情况的第${index + 1}条`);
							this.dataForm.tableField115.forEach((item, index) => {
								let jnpfFile = 'jnpfFiletb016' + index
								this.$refs[jnpfFile][0].$refs.lsjUpload.show()
							})
						}
					},
				});
			},

			hangledepData(data) {
				if (data) {
					let length = data.ids[0].length;
					let ids = data.ids[0];
					let str = data.name[0];
					let parts = str.split("/");
					// 检查数组是否为空，然后安全地获取最后一项
					this.lastItemId = length > 0 ? ids[length - 1] : null;
					this.lastItemName =
						parts.length === 1 ? parts[0] : parts[parts.length - 1];
				}
			},
			// 
			chioceOastdBox(index, type) {
				if (!this.dataForm.ta003) {
					this.$refs.uToast.show({
						title: "部门不能为空！",
						type: "error",
					});
					return;
				}
				this.index = index; //当前下标
				console.log("当前下标this.index", this.index);
				//报销部门
				var queryParam = {
					td006: this.dataForm.ta003,
					currentPage: 1,
					pageSize: 500,
					sort: "asc",
					sidx: "TD001",
				};
				getOastdList(queryParam).then((res) => {
					this.popupshow = true;
					this.tf004List = res.data.list.map((item, index) => {
						return {
							sid: index + 1,
							...item,
						};
					});
				});
			},
			selectOastdList(data) {
				this.dataForm.tableField112[this.index].tf004 = data.td001.trim();
				this.dataForm.tableField112[this.index].tf004Name = data.td002;
			},
			//选择退还款明细
			selectOastoList(list) {
				console.log("退还款明细");
				console.log(list);
				console.log(!this.list || list.length === 0);
				//退还预支款明细为空
				if (!list || list.length === 0) {
					this.dataForm.ta015 = 0;
				} else {
					//退还预支款金额汇总
					const totalTo009 = list.reduce(
						(sum, item) => sum + Number(item.to009 || 0),
						0
					);
					console.log("退还预支款金额汇总");
					console.log(totalTo009);
					this.dataForm.ta015 = this.jnpf.toDecimal(totalTo009);
				}
				//退还预支款明细
				this.dataForm.oastoList = list || [];
				this.refundSumChange();
			},
			//计算支付现金金额
			refundSumChange() {
				//支付现金金额=实际报销额-退还预支款金额
				var ta013 = parseFloat(this.dataForm.ta013 || 0);
				var ta015 = parseFloat(this.dataForm.ta015 || 0);
				this.dataForm.ta014 = this.jnpf.toDecimal(ta013 - ta015);
			},

			getInfoFn(data) {
				uni.$on('updateta016', (data) => {
					this.selectPersonBankInfoList(data)
				})
				uni.$on('updaterefund', (data) => {

					this.selectOastoList(data)
				})
				this.dataForm.flowId = data.flowId;
				getOastaBudgetNo().then((res) => {
					if (res.code == 200) {
						this.dataForm.ta004 = this.jnpf.getCurrentDate("YYYYMMDD");
						this.getUserInfo();
						this.getta006Options()
					}
				});
			},
			selfSubmit(eventType) {
				this.dataForm.status = eventType === "submit" ? 0 : 1;
				if (
					this.dataForm.tableField115.length <= 0 &&
					this.dataForm.oastjList.length <= 0
				) {
					return this.$refs.uToast.show({
						title: "单身资料不能为空",
						type: "error",
					});
				}
				if (this.dataForm.ta020 != this.dataForm.ta022) {
					return this.$refs.uToast.show({
						title: "[发票金额合计]必须与[报销金额合计]一致",
						type: "error",
					});
				}
				if (parseFloat(this.dataForm.ta013) > parseFloat(this.dataForm.ta020)) {
					return this.$refs.uToast.show({
						title: "[实际报销额]不能大于[报销金额合计]",
						type: "error",
					});
				}
				if (parseFloat(this.dataForm.ta015) > parseFloat(this.dataForm.ta019)) {
					return this.$refs.uToast.show({
						title: "[退还预支款金额]不能大于[借款余额]",
						type: "error",
					});
				}
				if (
					!this.dataForm.ta016 ||
					!this.dataForm.ta017 ||
					!this.dataForm.ta018
				) {
					return this.$refs.uToast.show({
						title: "收款人/开户行/银行账户不能为空",
						type: "error",
					});
				}
				if (!this.dataForm.ta006) {
					this.dataForm.ta015 = 0
					return this.$refs.uToast.show({
						title: "费用类型不能为空",
						type: "error",
					});
				}
				if (this.jnpf.isEmpty(this.dataForm.ta015)) {
					return this.$refs.uToast.show({
						title: "退还预支款金额不能为空",
						type: "error",
					});
				}
				//验证明细是否必填
				if (this.verify && !this.verify()) return;
				uni.showModal({
					title: "提示",
					content: "您确定要提交当前流程吗？",
					success: (res) => {
						if (res.confirm) {
							this.HandleSubmit(eventType);
						}
					},
				});
			},
			HandleSubmit(eventType) {
				this.dataForm.flowState = eventType == "submit" ? 0 : 1;
				console.log("this.dataForm", this.dataForm);
				postOasta(this.dataForm).then((res) => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit("refresh");
								uni.navigateBack();
							}, 1500);
						},
					});
				});
			},
		},
	};
</script>